<template>
  <div class="reset-container">
    <div class="reset-bg">
      <!-- LOGO -->
      <div class="logo-container">
        <img src="@/assets/images/logo.jpg" alt="全都有超市" class="logo-image">
        <h1 class="logo-text">全都有超市</h1>
      </div>

      <h2 class="reset-title">重置密码</h2>
      <p class="reset-desc">请输入新的密码以完成重置流程</p>

      <!-- 表单 -->
      <el-form ref="form" :model="passwordForm" :rules="rules" label-width="90px" class="reset-form">
  <el-form-item label="用户名" prop="username">
    <el-input v-model="passwordForm.username" placeholder="请输入用户名" />
  </el-form-item>
  <el-form-item label="新密码" prop="newPassword">
    <el-input v-model="passwordForm.newPassword" show-password placeholder="请输入新密码" />
  </el-form-item>
  <el-form-item label="确认密码" prop="confirmPassword">
    <el-input v-model="passwordForm.confirmPassword" show-password placeholder="请再次输入新密码" />
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="onSubmit" class="reset-btn" :loading="loading">
      <i class="el-icon-lock"></i>
      提交修改
    </el-button>
  </el-form-item>
</el-form>
    </div>
  </div>
</template>

<script>
import api from '@/api/login'
import { ElMessage } from 'element-plus'

export default {
  data() {
    return {
      passwordForm: {
        username: this.$route.params.username || '',
        newPassword: '',
        confirmPassword: ''
      },
      rules: {
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 2, max: 20, message: '密码长度在2到20个字符之间', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请再次输入新密码', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    async onSubmit() {
      try {
        const { newPassword, confirmPassword, username } = this.passwordForm

        if (newPassword !== confirmPassword) {
          ElMessage.error('两次输入的密码不一致')
          return
        }

        this.loading = true

     
        const response = await api.resetPassword({
          username,
          newPassword
        })

        if (response.data.code === 200) {
          ElMessage.success('密码修改成功！正在跳转登录页...')
          setTimeout(() => {
            this.$router.push('/login')
          }, 1500)
        } else {
          ElMessage.error(response.data.message || '密码修改失败')
        }
      } catch (error) {
        console.error('密码重置失败:', error)
        ElMessage.error('网络异常，请稍后再试')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
@primary-color: #ff4d4f;
@bg-white-transparent: rgba(255, 255, 255, 0.9);
@border-radius: 8px;

.reset-container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat center center fixed;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.reset-bg {
  width: 400px;
  padding: 40px 30px;
  border-radius: @border-radius;
  background-color: @bg-white-transparent;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.1);
  text-align: center;
  backdrop-filter: blur(10px);
}

.logo-container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 30px;

  .logo-image {
    width: 40px;
    height: 40px;
    margin-right: 10px;
    border-radius: 50%;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }

  .logo-text {
    font-size: 20px;
    color: @primary-color;
    font-weight: bold;
    letter-spacing: 1px;
    margin: 0;
  }
}

.reset-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.reset-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 30px;
}

.reset-form {
  width: 100%;

  .reset-btn {
    width: 100%;
    background-color: @primary-color;
    border-color: @primary-color;
    font-size: 16px;
    font-weight: 500;
    transition: all 0.3s ease;
    border-radius: @border-radius;

    &:hover {
      background-color: darken(@primary-color, 10%);
      border-color: darken(@primary-color, 10%);
    }

    i {
      margin-right: 8px;
    }
  }
}
</style>